<template>
  <el-cascader separator="-" :value="value" :options="depts" size="mini" :props="defaultProps" style="width: 100%" @change="changeValue" />
</template>
<script>
import { getDepartmentAPI } from '@/api'
import { transListToTreeData } from '@/utils'

export default {
  props: {
    value: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      defaultProps: {
        label: 'name',
        value: 'id'
      },
      depts: []
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      this.depts = transListToTreeData(await getDepartmentAPI(), 0)
    },
    changeValue(list) {
      if (list && list.length) {
        this.$emit('input', list[list.length - 1])
      } else {
        this.$emit('input', null)
      }
    }
  }
}
</script>

<style scoped>

</style>
